<%@page import="ll.sms.web.*"%><%@ page language="java"
	import="java.util.*,ll.sms.pojo.*,org.json.simple.*,java.io.*"
	pageEncoding="UTF-8"%>
<%@page import="ll.sms.util.*,org.apache.commons.lang.*"%>
<%



	String name = ParamUtils.getParameter(request,"username");
	name = name==null?"":new String(name.getBytes("ISO-8859-1"),"GBK");
	String toname = ParamUtils.getParameter(request,"tousername");
	toname = toname==null?"":new String(toname.getBytes("ISO-8859-1"),"GBK");
	String msendtime = ParamUtils.getParameter(request,"msendtime");
	String msendtime_1 = ParamUtils.getParameter(request,"msendtime_1");
	String channel = ParamUtils.getParameter(request,"channel_");
	String sendstatustr = ParamUtils.getParameter(request,"sendstatu");
	ExcelBuilder eb =new ExcelBuilder();
	eb.deleteAllFiles();
	int sendstatu = (sendstatustr==null||sendstatustr.trim().equals(""))?0:SmsUtil.toInt(sendstatustr);

	StringBuffer condition = null;
	String currentUsername = SmsUtil.sessionHelper(session,AdminProperty.auname);
	if(!"admin".equals(currentUsername)){
	//代理
	condition=new StringBuffer(" where mproxyid='"+SmsUtil.sessionHelper(session,AdminProperty.aid)+"' and");
	}else{
	condition=new StringBuffer(" where ");
	}
	
	if(!StringUtils.isBlank(name)){
		condition.append(" mfrom = '").append(name).append("' ");
	}else{
		condition.append(" 1=1 ");
	}
	condition.append("and");
	if(!StringUtils.isBlank(toname)){
		condition.append(" mto like '%").append(toname).append("%' ");
	}else{
		condition.append(" 1=1 ");
	}
	condition.append("and");
	if(!StringUtils.isBlank(msendtime) && !StringUtils.isBlank(msendtime_1)){
		condition.append(" msendtime >to_date('").append(msendtime).append("','yyyy-mm-dd hh24:mi:ss') ");
		condition.append("and").append(" msendtime<to_date('").append(msendtime_1).append("','yyyy-mm-dd hh24:mi:ss') ");
	}else{
		condition.append(" 1=1 ");
	}
	condition.append("and");
	if(!StringUtils.isBlank(channel)){
	condition.append(" mchannel = '" +channel+"' ");
	}else{
		condition.append(" 1=1 ");
	}
	condition.append("and");
	
		if(sendstatu ==-2){//失败
		condition.append(" mstatue = -2");
		}else if(sendstatu==1){//成功
			//其实成功还包括admin的取消
		condition.append(" mstatue = 1 or mstatue = -1");
		}else if(sendstatu==-1){//代理的取消
			condition.append(" mstatue = -3");
		}else{
			condition.append(" 1=1 ");
		}
		
	int total = WebBusinessImpl.getInstance().countTable("sms_message",
			condition.toString());

	 File f = eb.getExcel(total,condition.toString());


	 response.setContentType("text/html");
	 response.setHeader("Location",f.getName());

	 response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
	 OutputStream outputStream = response.getOutputStream();
	 InputStream inputStream = new FileInputStream(f);
	 byte[] buffer = new byte[1024];
	 int i = -1;
	 while ((i = inputStream.read(buffer)) != -1) {
	  outputStream.write(buffer, 0, i);
	  }
	 outputStream.flush();
	 outputStream.close();
	 inputStream.close();
	 outputStream = null;
	 out.clear();
     out = pageContext.pushBody();

	 System.out.print("over.....");

%>